package  com.diamond.action;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diamond.bo.MessageBO;
import com.diamond.bo.QueryMessageBO;
import com.diamond.bo.QueryUserBO;
import com.diamond.bo.ReturnBO;
import com.diamond.entity.Message;
import com.diamond.facade.MessageFacade;
import com.diamond.util.JsonMSG;
import com.diamond.util.JsonUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * MessageAction ������
 * @author JSmart Tools
 */
@SuppressWarnings("unchecked")
public class MessageAction extends ActionSupport {

	private static final long serialVersionUID = 173559139719842898L;
	private HttpServletRequest request = ServletActionContext.getRequest();
	private static Logger log=LoggerFactory.getLogger(MessageAction.class);
	private MessageFacade messageFacade;
	private List<MessageBO> dataList;
	private MessageBO messageBo;
	private Message message;
	private QueryMessageBO queryMessageBo;
	private String json;
	
	/**
	 * 发布和修改消息
	 * @return
	 * @throws Exception 
	 */
	public String addOrUpdateMessage() {
//		System.out.println("*************发布和修改消息****************");
		Map session = ActionContext.getContext().getSession();
		try {
			messageBo=(MessageBO) JsonUtils.toObject(json, MessageBO.class);
			messageBo.setAuthor((String)session.get("USER_NAME"));
			if ("钻石资料".equals(messageBo.getMsgTypeName())) {
				messageBo.setMsgType("1");
			} else if ("最新动态".equals(messageBo.getMsgTypeName())) {
				messageBo.setMsgType("2");
			} else {
				this.json=JsonMSG.failed("请选择消息类型").toJson();
			}
			messageFacade.addOrUpdateMessage(messageBo);
			this.json=JsonMSG.sucess("消息添加成功!").toJson();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("添加消息出错",e);
			this.json=JsonMSG.failed("添加消息出错!详细:"+e).toJson();
		}
			return "json";
	}
	
	/**
	 * 查询消息
	 * @return
	 */
	public String queryMessage() {
//		System.out.println("**************后台消息查询***************");
		request = ServletActionContext.getRequest();
		String start = request.getParameter("start");
		String pageSize = request.getParameter("limit");
		if(null==json|| "".equals(json.trim())||"null".equals(json.toLowerCase())){//默认查询全部;
			queryMessageBo=new QueryMessageBO();
		}
		else{
			queryMessageBo = (QueryMessageBO) JsonUtils.toObject(json, QueryMessageBO.class);
		}
		try {
			if ("0".equals(queryMessageBo.getMsgType())) {
				queryMessageBo.setMsgType("");
			}
			if (null == start || "".equals(start) || null == pageSize || "".equals(pageSize)) {
				queryMessageBo.setStart(0);
				queryMessageBo.setPageSize(15);
			} else {
				queryMessageBo.setStart(Integer.valueOf(start));
				queryMessageBo.setPageSize(Integer.valueOf(pageSize));
			}
			ReturnBO<MessageBO> returnBo = messageFacade.queryMessage(queryMessageBo);
			this.json=JsonMSG.sucess(returnBo).toJson();
		} catch (Exception e) {
			e.printStackTrace();
			this.json=JsonMSG.failed("查询失败").toJson();
			log.error("查询异常!",e);
		}
		return "json";
	}
	
    /**
     * 标题验证
     */
    public String checkMsgTitle(){
    	String msgTitle = json;
    	Boolean result;
		try {
			result = messageFacade.checkMsgTitle(msgTitle);
			if (result) {
	        	this.json=JsonMSG.sucess("恭喜您，该标题可用!").toJson();
	    	} else {
	        	this.json=JsonMSG.failed("该标题已存在，请您重新输入！").toJson();
	    	}
		} catch (Exception e) {
			this.json=JsonMSG.failed("该标题已存在，请您重新输入！").toJson();
			log.error("查询用户名出错",e);
		}
    	return "json";
    }
    
	/**
	 * 根据MessageId查询消息详细信息
	 * @return
	 */
	public String findMessageByMessageId() {
//		System.out.println("*******根据MessageId查询消息详细信息*********");
		try {
			String messageId=json;
			MessageBO returnMessageBo = messageFacade.findMessageByMessageId(messageId);
			this.json=JsonMSG.sucess(returnMessageBo).toJson();
		} catch (Exception e) {
			log.error("查询消息出错 ",e);
			this.json=JsonMSG.failed("系统未找到该 消息:"+json).toJson();
		}
		return "json";
	}
    
    /**
     * 删除消息
     * @return
     */
	public String deleteMessage() {
//		System.out.println("***********删除消息***********");
		try {
//			String messageId = json;
			String[] messageIds = json.split(";");
			if (messageIds.length > 0) {
				Boolean result = messageFacade.deleteMessage(messageIds);
				if (result) 
					this.json=JsonMSG.sucess("消息已删除!").toJson();
			} else {
				this.json = JsonMSG.failed("请选择需要删除的消息！").toJson();
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.json=JsonMSG.failed("删除消息出错!详细:"+e).toJson();
			log.error("删除消息出错!详细:",e);
		}
			return "json";
	}
	
//----------------------------------------------------------------------------
	/**
	 * 前台跳到消息详细页面
	 */
	public String toMessage(){
		request = ServletActionContext.getRequest();
		String id=request.getParameter("messageId");
		message=(Message) messageFacade.findById(new Long(id));
		
		request.setAttribute("content", message.getContent());
		return SUCCESS;
	}
	/**
	 * 前台跳到消息列表页面
	 */
	public String toMessList(){
		String url="tomesslist.action?a=1";
		String HQL="from Message where msgType='2' and delStatus='0'";
		if(messageBo!=null){
			if(!StringUtils.isEmpty(messageBo.getName())){
				try {
					String name=URLDecoder.decode(messageBo.getName(),"UTF-8");
					messageBo.setName(name);
					url+="&messageBo.name="+name;
					HQL+=" and name like '%"+name+"%'";
				} catch (Exception e) {
					e.printStackTrace();
				}
			
			}
		}
		dataList = Page.util(ServletActionContext.getRequest(),url,messageFacade,HQL,20);
		return SUCCESS;
	}
	/**
	 * 前台跳到知识列表页面
	 */
	public String toKnowList(){
		String url="toknowlist.action?a=1";
		String HQL="from Message where msgType='1' and delStatus='0'";
		if(messageBo!=null){
			if(!StringUtils.isEmpty(messageBo.getName())){
				try {
					String name=URLDecoder.decode(messageBo.getName(),"UTF-8");
					messageBo.setName(name);
					url+="&messageBo.name="+name;
					HQL+=" and name like '%"+name+"%'";
				} catch (Exception e) {
					e.printStackTrace();
				}
			
			}
		}
		dataList = Page.util(ServletActionContext.getRequest(),url,messageFacade,HQL,20);
		return SUCCESS;
	}

	public MessageBO getMessageBo() {
		return messageBo;
	}

	public void setMessageBo(MessageBO messageBo) {
		this.messageBo = messageBo;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public void setDataList(List<MessageBO> dataList) {
		this.dataList = dataList;
	}

	public List<MessageBO> getDataList() {
		return dataList;
	}

	public void setMessageFacade(MessageFacade messageFacade) {
		this.messageFacade = messageFacade;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public QueryMessageBO getQueryMessageBo() {
		return queryMessageBo;
	}

	public void setQueryMessageBo(QueryMessageBO queryMessageBo) {
		this.queryMessageBo = queryMessageBo;
	}
	
}
